/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validators;

import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Pablo
 */
@FacesValidator (value="numDiasValidator")
public class numDiasValidator implements Validator{

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) 
            throws ValidatorException {
        //String attribute = (String)component.getAttributes().get("ndias");
        Integer attribute = (Integer) component.getAttributes().get("ndias");
        //String attribute = value.toString();
        //String attribute = attribute2.toString();
        /*
        Integer ndiasmin;
        try{
            ndiasmin = new Integer(attribute.substring(0, attribute.indexOf("-")));
        }catch(Exception e){
            ndiasmin = null;
        }
        Integer ndiasmax;
        try{
            ndiasmax = new Integer(attribute.substring(attribute.indexOf("-")+1, attribute.length()));
        }catch(Exception e){
            ndiasmax = null;
        }
        * *
        */
        if ((value == null && attribute != null) || (value != null && attribute == null)){
            FacesMessage message = new FacesMessage();
            message.setSummary("No se puede especificar sólo un límite de días");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }        
        if ((value != null && attribute != null) && (attribute.compareTo((Integer)value)>0)){
            FacesMessage message = new FacesMessage();
            message.setSummary("El número de días de la izquierda debe ser menor que el de la derecha");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }
    
}
